package com.step.math.bak;

import android.util.FloatMath;

import com.step.utils.MathUtils;

public class Vector2D{
	public float x;
	public float y;
	
	public Vector2D() {
		this.x = this.y = 0;
	}
	
	public Vector2D(float x, float y){
		this.x = x;
		this.y = y;
	}

	public Vector2D set(float x, float y){
		this.x = x;
		this.y = y;
		return this;
	}

	public Vector2D set(Vector2D vector){
		this.x = vector.x;
		this.y = vector.y;
		return this;
	}

	public Vector2D add(Vector2D vector) {
		this.x += vector.x;
		this.y += vector.y;
		return this;
	}

	public Vector2D add(float x, float y){
		this.x += x;
		this.y += y;
		return this;
	}

	public Vector2D sub(Vector2D vector) {
		this.x -= vector.x;
		this.y -= vector.y;
		return this;
	}

	public Vector2D sub(float x, float y){
		this.x -= x;
		this.y -= y;
		return this;
	}

	public Vector2D mul(float scale) {
		this.x *= scale;
		this.y *= scale;
		return this;
	}
	
	public Vector2D div(float scale){
		this.x /= scale;
		this.y /= scale;
		return this;
	}

	public Vector2D copy() {
		return new Vector2D(x, y);
	}

	public float len() {
		return FloatMath.sqrt(lenSQ());
	}

	public float lenSQ(){
		return x * x + y * y;
	}
	
	public float distSQ(Vector2D vector){
		float dx = x - vector.x;
		float dy = y - vector.y;
		
		return dx * dx + dy * dy;
	}

	public float distSQ(float x, float y){
		float dx = this.x - x;
		float dy = this.y - y;
		
		return dx * dx + dy * dy;
	}

	public float dist(float x, float y){
		return FloatMath.sqrt(distSQ(x, y));
	}
	
	public float dist(Vector2D vector){
		return FloatMath.sqrt(distSQ(vector));
	}

	/**
	 * 叉积：值等于两向量的模与其夹角的正弦值相乘，表示以两向量为边组成的平行四边形的面积，a x b = |a| * |b| * sinQ 
	 * @param vector
	 * @return
	 */
	public float cro(Vector2D vector) {
		return x * vector.y - y * vector.x;
	}

	/**
	 * 点积：值等于两向量的模与其夹角的余弦值相乘，表示两向量的角度关系(cos的值)， a . b = |a| * |b| * cosQ
	 * @param vector
	 * @return
	 */
	public float dot(Vector2D vector) {
		return x * vector.x + y * vector.y;
	}

	/**
	 * 向量的角度（0-360）（注意：这里是角度，不是弧度）
	 * @return
	 */
	public float angle(){
		float angle = MathUtils.atan2(y, x) * MathUtils.radiansToDegrees;
		if (angle < 0) angle += 360;
		return angle;
	}
	
	/**
	 * 旋转
	 * @param angle 角度（0-360）
	 * @return
	 */
	public Vector2D rotate (float angle) {
		float rad = angle * MathUtils.degreesToRadians;
		float cos = MathUtils.cos(rad);
		float sin = MathUtils.sin(rad);

		float newX = this.x * cos - this.y * sin;
		float newY = this.x * sin + this.y * cos;

		this.x = newX;
		this.y = newY;

		return this;
	}

	public boolean isUnit(){
		return len() == 1;
	}

	public boolean isZero(){
		return this.x == 0 && this.y == 0;
	}

	public boolean isEqual(Vector2D vector){
		return this.x == vector.x && this.y == vector.y;
	}
	
	/**
	 * 向量标准化
	 * @return
	 */
	public Vector2D nor(){
		float len = len();
		
		if(len != 0){
			x /= len;
			y /= len;
		}
		
		return this;
	}
	
	public static Vector2D add(Vector2D v1, Vector2D v2){
		return new Vector2D(v1.x + v2.x, v1.y + v2.y);
	}
	
	public static Vector2D sub(Vector2D v1, Vector2D v2){
		return new Vector2D(v1.x - v2.x, v1.y - v2.y);
	}
	
	public static Vector2D mul(Vector2D v1, Vector2D v2){
		return new Vector2D(v1.x * v2.x, v1.y * v2.y);
	}
	
	public static float dot(Vector2D v1, Vector2D v2){
		return v1.x * v2.x + v1.y * v2.y;
	}
	
	public static float cro(Vector2D v1, Vector2D v2){
		return v1.x * v2.y - v2.x * v1.y;
	}
}
